#include <stdlib.h>
#include "glut_window.h"

namespace venus {
GlutWindow::GlutWindow(
  const char* title /*= "Glut Window"*/,
  int x /*= -1*/, int y /*= -1*/, 
  int width /*= 800*/, int height /*= 600*/, 
  unsigned int mode /*= GLUT_RGBA|GLUT_DOUBLE */)
  : LogicWindow(x, y, width, height) {
  glutInitDisplayMode(mode);
  glutInitWindowSize(this->width(), this->height());
  if (this->x() != -1 || this->y() != -1) 
    glutInitWindowPosition(this->x(), this->y());
  win_id_ = glutCreateWindow(title);
  if (win_id_ >= kMaxGlutWindowCount) exit(0);
  if (win_id_ > 0) {
    //save a pointer to the stucture
    s_windows[win_id_] = this;
    if (!s_glut_helper_initialized) {
      glh::glut_helpers_initialize();
      s_glut_helper_initialized = true;
    }
    call_backs_.keyboard_function = keyboard;
    call_backs_.special_function = specialkey;
    call_backs_.display_function = display;
    call_backs_.reshape_function = reshape;
    call_backs_.idle_function = idle;
    call_backs_.motion_function = motion;
    call_backs_.mouse_function = button;
    call_backs_.visibility_function = visibility;
    call_backs_.passive_motion_function = passive_motion;
    call_backs_.timer_function = timer;
    call_backs_.menu_status_function = menu_status;
    glh::glut_add_interactor(&call_backs_);
  }
}

GlutWindow::~GlutWindow(void) {
  if (win_id_ > 0) {
    glutDestroyWindow(win_id_);
    s_windows[ win_id_ ] = NULL;
  }
}	

GlutWindow* GlutWindow::s_windows[kMaxGlutWindowCount];
bool GlutWindow::s_glut_helper_initialized = false;
}
